﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;

using ChiDao.Component.Utils;
using ChiDao.Business.Models;
using ChiDao.Business.Models.FlowProcess;
using ChiDao.Site.Models;
using ChiDao.Business.Data.Repositories.FlowProcess;


namespace ChiDao.Business.Service.Impl
{
    [Export(typeof(IHolidayDocumentService))]
    internal class HolidayDocumentService : IHolidayDocumentService
    {
        [Import]
        protected IHolidayDocumentRepository HolidayDocumentRepository { get; set; }
        [Import]
        protected IBaklogTaskRepository BaklogTaskRepository { get; set; }

        public HolidayDocumentView QueryHolidayDocument(string id, string userName)
        {
            HolidayDocumentView holidayDocumentView = new HolidayDocumentView();
            if (!string.IsNullOrEmpty(id))
            {
                Guid queryID = Guid.Parse(id);
                IQueryable<HolidayDocument> holidayDocuments = HolidayDocumentRepository.Entities.Where(h => h.ID == queryID);
                if (holidayDocuments.Count() == 1)
                {
                    HolidayDocument holidayDocument = holidayDocuments.First();
                    holidayDocumentView.ID = holidayDocument.ID;
                    holidayDocumentView.RequestPerson = holidayDocument.RequestPerson;
                    holidayDocumentView.RequestDate = holidayDocument.RequestDate;
                    holidayDocumentView.Description = holidayDocument.Description;
                    holidayDocumentView.HandleState = holidayDocument.HandleState;

                    IQueryable<BaklogTask> baklogTasks = BaklogTaskRepository.Entities.Where(b => b.ProcessID == id && b.HandleState == "1").OrderBy(b => b.ProcessTime);
                    foreach (BaklogTask baklogTask in baklogTasks)
                    {
                        holidayDocumentView.lstBaklogTask.Add(new BaklogTaskView() { ID = baklogTask.ID, ProcessID = baklogTask.ProcessID, TaskName = baklogTask.TaskName, RequestPerson = baklogTask.RequestPerson, ProcessTime = baklogTask.ProcessTime, CurrentProcessNodeCode = baklogTask.CurrentProcessNodeCode, CurrentProcessNodeName = baklogTask.CurrentProcessNodeName, NextProcessNodeCode = baklogTask.NextProcessNodeCode, ApprovalOpinion = baklogTask.ApprovalOpinion, CurrentProcessPerson = baklogTask.CurrentProcessPerson });
                    }
                }
                else
                {
                    holidayDocumentView.ID = Guid.NewGuid();
                    holidayDocumentView.RequestPerson = userName;
                    holidayDocumentView.RequestDate = DateTime.Now;
                    holidayDocumentView.HandleState = "0";
                }
            }
            else
            {
                holidayDocumentView.ID = Guid.NewGuid();
                holidayDocumentView.RequestPerson = userName;
                holidayDocumentView.RequestDate = DateTime.Now;
                holidayDocumentView.HandleState = "0";
            }
            return holidayDocumentView;
        }

        public bool OnlyPreviewHolidayDocument(string id)
        {
            bool isOnlyPreview = false;
            if (!string.IsNullOrEmpty(id))
            {
                Guid queryID = Guid.Parse(id);
                IQueryable<HolidayDocument> holidayDocuments = HolidayDocumentRepository.Entities.Where(h => h.ID == queryID);
                if (holidayDocuments.Count() == 1)
                {
                    HolidayDocument holidayDocument = holidayDocuments.First();
                    if (holidayDocument.HandleState != "0")
                    {
                        isOnlyPreview = true;
                    }
                }
            }
            return isOnlyPreview;
        }

        public void UpdateHolidayDocument(Guid id, DateTime requestDate, string description, string handleState)
        {
            HolidayDocument record = HolidayDocumentRepository.Entities.FirstOrDefault(m => m.ID == id);
            if (record != null)
            {
                if (handleState == "0")
                {
                    record.RequestDate = requestDate;
                    record.Description = description;
                }
                record.HandleState = handleState;
                HolidayDocumentRepository.Update(record);
            }
        }

        public void SaveHolidayDocument(Guid id, string userName, DateTime requestDate, string description)
        {
            HolidayDocument record = HolidayDocumentRepository.Entities.FirstOrDefault(m => m.ID == id);
            if (record != null)
            {
                UpdateHolidayDocument(id, requestDate, description,"0");
            }
            else
            {
                record = new HolidayDocument() { ID = id, RequestPerson = userName, RequestDate = requestDate, Description = description, HandleState = "0" };
                HolidayDocumentRepository.Insert(record);
            }
        }

        public List<HolidayDocumentView> QueryRequestHolidayDocument(string userName)
        {
            List<HolidayDocumentView> holidayDocumentViewList = new List<HolidayDocumentView>();
            IQueryable<HolidayDocument> holidayDocuments = HolidayDocumentRepository.Entities.Where(h => h.RequestPerson == userName);
            foreach (HolidayDocument holidayDocument in holidayDocuments)
            {
                holidayDocumentViewList.Add(new HolidayDocumentView() { ID = holidayDocument.ID, RequestPerson = holidayDocument.RequestPerson, RequestDate = holidayDocument.RequestDate, Description = holidayDocument.Description, HandleState = holidayDocument.HandleState });
            }
            return holidayDocumentViewList;
        }

        public HolidayDocumentView GetHolidayDocumentByID(string id)
        {
            HolidayDocumentView holidayDocumentView = null;
            Guid queryID = Guid.Parse(id);
            IQueryable<HolidayDocument> holidayDocuments = HolidayDocumentRepository.Entities.Where(h => h.ID == queryID);
            if (holidayDocuments.Count() == 1)
            {
                HolidayDocument holidayDocument = holidayDocuments.First();
                holidayDocumentView = new HolidayDocumentView();
                holidayDocumentView.ID = holidayDocument.ID;
                holidayDocumentView.RequestPerson = holidayDocument.RequestPerson;
                holidayDocumentView.RequestDate = holidayDocument.RequestDate;
                holidayDocumentView.Description = holidayDocument.Description;
                holidayDocumentView.HandleState = holidayDocument.HandleState;
            }
            return holidayDocumentView;
        }
    }
}